<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText" @tap="onreturn()">返回</block>
			<block slot="content">商品出库</block>
		</cu-custom>

		<view class="margin-top">
			<form id="eform">
				<view class="cu-form-group margin-top">
					<view class="title">商品名称</view>
					<picker @change="PickerChange" :value="index" :range="pickercustom">
						<view class="picker" v-model="userdata.goodname" name="goodname">

							{{index>-1?pickercustom[index]:'请选择商品'}}
						</view>
					</picker>
				</view>

				<view class="cu-form-group ">
					<view class="title  from_width">出库数量</view>
					<input type="number" v-model="userdata.num" placeholder="请输入出库数量" name="num"></input>
				</view>
				<view class="cu-form-group">
					<view class="title from_width">出库人</view>
					<input type="text" v-model="userdata.addby" placeholder="请输入出库人" name="addby"></input>
				</view>
				<view class="cu-form-group">
					<view class="title from_width">出库时间</view>
					<input type="text" v-model="userdata.cdate" placeholder="请输入出库时间" name="cdate"></input>
				</view>
				<view class="bg-white align-center">
					<view class="flex flex-wrap">
						<view class="basis-df padding">
							<button class="bg-blue xs text-lg" @tap="tj()">提交</button>
						</view>
						<view class="basis-df  padding">
							<button class="bg-blue xs text-lg" form-type="reset" refBtn>清空</button>
						</view>
					</view>
				</view>
			</form>
		</view>
	</view>
</template>

<script>
	import uu from "@/common/util.js";

	export default {
		data() {
			return {
				op_type: '',
				index: -1,
				pickercustom: [],//商品列表
				pickerids:[],//商品库存
				userdata: {
					//id: '0',
					goodid: '',
					goodname: '',
					num: '0',
					addby: '',
					cdate: uu.getTime(),
				},
				date: '2018-12-25',
			}
		},
		onLoad() {
			this.getgoods();
		},
		onReady() {},
		methods: {
			DateChange(e) {
				this.date = e.detail.value
			},
			PickerChange(e) {
				console.log(e.detail);
				this.index = e.detail.value;

			},
			// 返回上一页
			onreturn() { 
			},
			backButton() {
				uni.navigateBack({
					delta: 1
				});
			},
			//录入出库记录
			tj() {
				var _self = this;

				//验证是否选择 商品 
				if (this.index < 0) {
					uni.showToast({
						title: '请选择商品！',
						icon: 'none'
					});
					return;
				}


				uni.showLoading({
					title: '提交中...'
				})
				_self.userdata.goodname = _self.pickercustom[_self.index]; 
				console.log(_self.pickerids[_self.index])
				//云数据库，更新库存，并生成历史记录 
				uniCloud.callFunction({ 
					name: "upd_good", 
					data:{
						ctype:'output',
						name:_self.pickercustom[_self.index],
						num : _self.pickerids[_self.index]-_self.userdata.num,
						record: _self.userdata
					}
				}).then((res) => {
					const {
						result
					} = res
					if(result.code==200){
						uni.showToast({
							title: '添加成功！',
							icon: "success"
						});
						console.log('executeSql success!'); 
					}
					uni.hideLoading();
				});
				//录入通知公告
				var cdata = {
					title:_self.userdata.addby+"出库"+_self.pickercustom[_self.index] + "[数量:" + _self.userdata.num+"]",
					cdate:uu.getTime()
				}
				uniCloud.callFunction({
					name: "add_note", 
					data: cdata
				})
				 
				_self.backButton();
			},
			clearForm() {
				//h5端可以 //翻源码得知的清空表单方法
				console.log('触发清空表单事件');
				this.$refs.refBtn.$dispatch('Form', 'uni-form-reset', {
					type: 'reset'
				})
			},
			//获取商品列表
			getgoods() {
				var _self = this;
				_self.pickercustom = [];
				//读取云商品
				uniCloud.callFunction({
					name: "get_goodinfo",
					data:{
						 
					}
				}).then((res) => {
					const {
						result
					} = res
					for (let i = 0; i < result.data.length; i++) {
						var item = result.data[i];
						_self.pickercustom.push(item.name);
						_self.pickerids.push(item.num);
					}

				});


			},


		}
	}
</script>

<style>
	page {
		width: 100vw;
		position: relative;
		background-color: #fff;
	}

	.from_width {
		width: 200upx;
	}
</style>
